package com.wolfalpha.lwtool.activity;

import com.wolfalpha.lwtool.R;
import com.wolfalpha.lwtool.constant.Application;
import android.widget.*;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

	int getNum = 0;
	ProgressDialog pd = null;
	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		@SuppressWarnings("deprecation")
		public void handleMessage(Message msg) {
			if (pd == null)
				return;
			switch (msg.what) {
			case 0:
				pd.setMessage((String) msg.obj + "\n点击取消停止任务");
				if (pd.isShowing()) {
					break;
				}
				pd.setTitle("正在执行....GET(" + msg.arg1 + ")");
				pd.setCancelable(false);
				pd.setButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						Application.getBaseService(MainActivity.this).stopWork(
								true);
						x = false;
					}
				});
				pd.show();
				break;
			case 1:
				getNum++;
				if (pd.isShowing()) {
					pd.setTitle("正在执行....GET(" + getNum + ")");
					break;
				}
				break;
			}
			super.handleMessage(msg);
		}
	};
	boolean x = false;

	/**
	 * 在界面恢复到最上面时调用
	 */
	@Override
	protected void onResume() {
		// 设置当前活动状态
		Application.setFrontActivity(Application.N_MAIN);
		Log.d("当前活动：", Application.N_MAIN);
		super.onResume();
	}

	/**
	 * 销毁当前界面时要关闭广播
	 */
	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mBroadcastReceiver);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);

		pd = new ProgressDialog(this);
		// 注册广播
		registerBoradcastReceiver();

		((RelativeLayout) findViewById(R.id.ly_setting))
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						startActivity(new Intent(MainActivity.this,
								SettingActivity.class));
					}
				});

		((RelativeLayout) findViewById(R.id.ly_static_get))
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {

						// Toast.makeText(MainActivity.this, "抓取已关闭!",
						// Toast.LENGTH_SHORT).show();
						// //
						String mid[] = new String[] { "手动抓取", "自动抓取" };
						AlertDialog.Builder builder = new AlertDialog.Builder(
								MainActivity.this);
						builder.setItems(mid,
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										if (which == 0) {
											if (!x)
												startActivity(new Intent(
														MainActivity.this,
														DynamicZQActivity.class));
											else {
												Toast.makeText(
														MainActivity.this,
														"您正在运行静态抓取",
														Toast.LENGTH_SHORT)
														.show();
											}
										} else {
											// x = true;
											// getNum = 0;
											// Application.getBaseService(MainActivity.this).stayZQ();
											Toast.makeText(MainActivity.this,
													"该功能已关闭，请选择手动抓取！",
													Toast.LENGTH_SHORT).show();
										}
									}
								});
						builder.create().show();
					}
				});
		((RelativeLayout) findViewById(R.id.ly_history))
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						startActivity(new Intent(MainActivity.this,
								HistoryActivity.class));
					}
				});

		((RelativeLayout) findViewById(R.id.ly_count))
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						startActivity(new Intent(MainActivity.this,
								CountActivity.class));
					}
				});
		((RelativeLayout) findViewById(R.id.ly_my))
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						startActivity(new Intent(MainActivity.this,
								MyJobActivity.class));
					}
				});
	}

	/**
	 * 注册广播
	 */
	private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(Application.N_MAIN)) {
				Message msg = new Message();
				msg.what = 0;
				msg.obj = intent.getStringExtra("msg");
				if (String.valueOf(msg.obj).equals("GET")) {
					msg.what = 1;
				}
				handler.sendMessage(msg);
			}
		}

	};

	public void registerBoradcastReceiver() {
		IntentFilter myIntentFilter = new IntentFilter();
		myIntentFilter.addAction(Application.N_MAIN);
		// 注册广播
		registerReceiver(mBroadcastReceiver, myIntentFilter);
	}

}
